// JavaScript Document

function ad_dialog(id)
{
	window["dialog_" + id] = new Object();
	window["dialog_" + id].id = id;
	window["dialog_" + id].left = -1;
	window["dialog_" + id].top = -1;
	window["dialog_" + id].right = -1;
	window["dialog_" + id].is_min = true;
	window["dialog_" + id].all_step = 20;
	
	this.location = function(left, top, right)
	{
		window["dialog_" + id].left = left;
		window["dialog_" + id].top = top;
		window["dialog_" + id].right = right;
	}
	
	this.max_size = function(width, height)
	{
		window["dialog_" + id].max_size = new Rect(width, height);
		window["dialog_" + id].size = new Rect(width, height);
	}
	
	this.min_size = function(width, height)
	{
		window["dialog_" + id].min_size = new Rect(width, height);
	}
	
	this.enlarge = function()
	{
		window["dialog_" + id].enlarge();
	}
	
	window["dialog_" + id].enlarge = function()
	{
		if(!window["dialog_" + id].dir && window["dialog_" + id].is_min)
		{
			window["dialog_" + id].dir = "enlarge";
			window["dialog_" + id].step = window["dialog_" + id].all_step;
			window["dialog_" + id].start();
		}
	}
	
	this.reduce = function()
	{
		window["dialog_" + id].reduce();
	}
	
	window["dialog_" + id].reduce = function()
	{
		if(!window["dialog_" + id].dir && !window["dialog_" + id].is_min)
		{
			window["dialog_" + id].dir = "reduce";
			window["dialog_" + id].step = window["dialog_" + id].all_step;
			window["dialog_" + id].start();
		}
	}
	
	this.start = function()
	{
		window["dialog_" + id].start();
	}
	
	window["dialog_" + id].start = function()
	{
		this.timer = setInterval(this.change_size, 10);
	}
	
	window["dialog_" + id].change_width = function()
	{
		return this.max_size.width - this.min_size.width;
	}
	window["dialog_" + id].change_height = function()
	{
		return this.max_size.height - this.min_size.height;
	}
	/*
	this.change_size = function()
	{
		window["dialog_" + id].change_size();
	}
	*/
	window["dialog_" + id].change_size = function()
	{
		//alert(window["dialog_" + id].id);

		var e = $("#" + window["dialog_" + id].id);
		
		if(e)
		{
			var l = e.offset();
			var left = l.left;
			var top = l.top;
			window["dialog_" + id].step = window["dialog_" + id].step - 1;
			
			if(window["dialog_" + id].dir == "enlarge")
			{
				var window_width = $(window).width();
		        var window_height = $(window).height();
				var change_top = (window_height > window["dialog_" + id].max_size.height ? Math.floor((window_height - window["dialog_" + id].max_size.height) /2 ) : 10) - top;
				var change_left = (window_width > window["dialog_" + id].max_size.width ? Math.floor((window_width - window["dialog_" + id].max_size.width) /2 ) : 10) - left;
				
				
				if(window["dialog_" + id].step > 0)
				{
					//change_top = Math.floor(change_top / window["dialog_" + id].step + top);
					//change_top = Math.floor(change_top / window["dialog_" + id].step);
					
					e.css("top", Math.floor(change_top / window["dialog_" + id].step + top) + $(window).scrollTop());
					e.css("left", Math.floor(change_left / window["dialog_" + id].step + left));
					e.css("width", Math.floor(window["dialog_" + id].change_width() * (window["dialog_" + id].all_step - window["dialog_" + id].step) / window["dialog_" + id].all_step) + window["dialog_" + id].min_size.width);
					e.css("height", Math.floor(window["dialog_" + id].change_height() * (window["dialog_" + id].all_step - window["dialog_" + id].step) / window["dialog_" + id].all_step) + window["dialog_" + id].min_size.height);
				}
				else
				{
					window["dialog_" + id].dir = null;
					window["dialog_" + id].is_min = false;
					window["dialog_" + id].update();
					clearInterval(window["dialog_" + id].timer);
					//window["dialog_" + id].reduce();
				}
			}
			else if(window["dialog_" + id].dir == "reduce")
			{
				var window_width = $(window).width();
				var change_top = window["dialog_" + id].top - top;
				var change_left = (window["dialog_" + id].left >= 0 ? window["dialog_" + id].left : (window_width - window["dialog_" + id].right -  window["dialog_" + id].min_size.width)) - left;
				//alert("window_width:" + window_width + "change_left:" + change_left + "left:" + left);
				if(window["dialog_" + id].step > 0)
				{

					e.css("top", Math.floor(change_top / window["dialog_" + id].step + top) + $(window).scrollTop());
					e.css("left", Math.floor(change_left / window["dialog_" + id].step + left));
					e.css("width", Math.floor(window["dialog_" + id].change_width() * (window["dialog_" + id].step) / window["dialog_" + id].all_step) + window["dialog_" + id].min_size.width);
					e.css("height", Math.floor(window["dialog_" + id].change_height() * (window["dialog_" + id].step) / window["dialog_" + id].all_step) + window["dialog_" + id].min_size.height);
				} 
				else
				{
					window["dialog_" + id].dir = null;
					window["dialog_" + id].is_min = true;
					window["dialog_" + id].update();
					clearInterval(window["dialog_" + id].timer);
				}
			}
			
		}
	}
	
	window["dialog_" + id].auto_update = function()
	{
		if(!window["dialog_" + id].dir)
		{
			alert("asd");
			setTimeout(window["dialog_" + id].update, 10);
		}
	}
	
	window["dialog_" + id].update = function()
	{
		try
		{
			var window_width = $(window).width();
			var window_height = $(window).height();
			e = $("#" + window["dialog_" + id].id);
			if(window["dialog_" + id].is_min)
			{
				
				e.css("width", window["dialog_" + id].min_size.width);
				e.css("height", window["dialog_" + id].min_size.height);
				e.css("top", window["dialog_" + id].top  + $(window).scrollTop());

				if(window["dialog_" + id].left >= 0)
				{
					e.css("left", window["dialog_" + id].left);
				}
				
				if(window["dialog_" + id].right >= 0)
				{
					e.css("right", window["dialog_" + id].right);
				}
			}
			else
			{
				e.css("width", window["dialog_" + id].max_size.width);
				e.css("height", window["dialog_" + id].max_size.height);
				var width = e.width();
				var height = e.height();
				
				if(window_height > height)
				{
					var e_top = Math.floor((window_height - height) /2 );
				}
				else
				{
					var e_top = 10;
				}
				
				if(window_width > width)
				{
					var e_right = Math.floor((window_width - width) /2 );
				}
				else
				{
					var e_right = 10;
				}
				e.css("top", e_top + $(window).scrollTop());
				e.css("right", e_right);
			}
			//alert("asdas");
		}
		catch(err)
		{
			alert(err.description);
		}
	}
	
	$(document).ready(function(){
							   $(window).scroll(window["dialog_" + id].auto_update).resize(window["dialog_" + id].auto_update);
							   window["dialog_" + id].update();
							   $("#" + id).hover(window["dialog_" + id].enlarge, window["dialog_" + id].reduce);
							   });
}

function Rect(w, h)
{
	this.width = w;
	this.height = h;
}
